package pswvclass.pswvandroid2.com.user;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import pswvclass.pswvandroid2.R;
import pswvclass.pswvandroid2.com.PswvActivity;
import pswvclass.pswvandroid2.com.util.DBHelper;
import pswvclass.pswvandroid2.com.util.HttpUtils;

/**
 * Created by Administrator on 2018/12/18.
 */
public class SafeSetInner extends AppCompatActivity {

    private String tag = "SafeSet";
    private TextView password_question;
    private TextView password_key;
    private DBHelper db;
    private SQLiteDatabase sd;
    protected static final int SUCCESS = 0;
    protected static final int ERROR = 1;
    protected static final int SAFE_SET = 2;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.pswv_password_safe_set_inner);
        password_question = (TextView) findViewById(R.id.password_question);
        password_key = (TextView) findViewById(R.id.password_key);
        onLoad();
    }


    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SUCCESS: {
                    JSONObject jsonObj = (JSONObject) msg.obj;
                    try {
                        String question = jsonObj.getString("passwordQuestion");
                        String key = jsonObj.getString("passwordKey");
                        password_question.setText(question);
                        password_key.setText(key);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                }
                case SAFE_SET: {
                    Intent intent = new Intent();
                    intent.setClass(SafeSetInner.this, SafeSet.class);
                    startActivity(intent);
                    break;
                }
                case ERROR: {
                    break;
                }
                default:
                    break;
            }

        }

        ;

    };

    public void onLoad() {
        DBHelper db = new DBHelper(this);
        SQLiteDatabase sd = db.getWritableDatabase();
        int userId = 0;
        Cursor cursor = sd.query("user", new String[]{"userId"}, "state=1", null, null, null, null);
        while (cursor.moveToNext()) {
            userId = cursor.getInt(0);
        }
        cursor.close();
        final int finalUserId = userId;
        new Thread() {
            public void run() {
                try {
                    HttpUtils hu = new HttpUtils();
                    String data2;
                    final String src = getResources().getString(R.string.src_mysql) + "pswv/api/userAction/safeSetInner";
                    final Map<String, String> map1 = new HashMap<String, String>();
                    map1.put("id", finalUserId + "");
                    data2 = hu.submitPostData(src, map1, "utf-8");
                    JSONObject jsonObj = new JSONObject(data2);
                    int result = Integer.parseInt(jsonObj.getString("code"));
                    if (result == 0) {
                        JSONObject jsonObj1 = jsonObj.getJSONObject("response");
                        Message msg = Message.obtain();
                        msg.obj = jsonObj1;
                        msg.what = SUCCESS;
                        mHandler.sendMessage(msg);
                    } else if (result == 21) {
                        Message msg = Message.obtain();
                        msg.what = SAFE_SET;
                        mHandler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Message msg = Message.obtain();
                    msg.what = ERROR;
                    mHandler.sendMessage(msg);
                    Log.v(tag, e + "");
                }
            }
        }.start();
    }
}
